import Vue from 'vue'
import Router from 'vue-router'
import i18n from '../i18n'
import store from '../store'
import formatParam from '../util/formatParam'
import { urlParse } from '../util/tool'

const files = require.context('./', true, /.js$/)
let routers = []
files.keys().forEach(item => {
  if (item !== './index.js') {
    const arr = files(item).default
    routers = routers.concat(arr)
  }
})
/**
 * 重写路由的push方法 带全局参数
 *
 */
const routerPush = Router.prototype.push
Router.prototype.push = function push (location, onComplete, onAbort) {
  let params = {
    userId: 1 || ''
  }
  if (store.state.appId) params.appId = store.state.appId
  if (typeof location === 'string') {
    const obj = urlParse(location)
    location = location.split('?')[0]
    params = {
      ...obj,
      ...params
    }
    // 路径为字符串 有?问号 并且后面有参数 拼接参数 &
    if (/\?.{1}/.test(location)) {
      location = location + '&' + formatParam(params)
    } else if (/\?/.test(location)) {
      // 拼接参数直接是在 ? 号后面了
      location = location + formatParam(params)
    } else {
      // 没有 ? 号
      location = location + '?' + formatParam(params)
    }
  } else {
    location = {
      ...location,
      query: {
        ...(location.query || {}),
        ...params
      }
    }
  }
  return routerPush.call(this, location, onComplete, onAbort)
}

/**
 * 重写路由的 replace 方法 带全局参数
 *
 */
const routerReplace = Router.prototype.replace
Router.prototype.replace = function replace (location, onComplete, onAbort) {
  const { orderCustId, parentCustId, userId } = store.state.userInfo
  const params = {
    orderCustId: orderCustId || '',
    parentCustId: parentCustId || '',
    userId: userId || ''
  }
  if (store.state.appId) params.appId = store.state.appId
  if (typeof location === 'string') {
    // 路径为字符串 有?问号 并且后面有参数 拼接参数 &
    if (/\?.{1}/.test(location)) {
      location = location + '&' + formatParam(params)
    } else if (/\?/.test(location)) {
      // 拼接参数直接是在 ? 号后面了
      location = location + formatParam(params)
    } else {
      // 没有 ? 号
      location = location + '?' + formatParam(params)
    }
  } else {
    location = {
      ...location,
      query: {
        ...(location.query || {}),
        ...params
      }
    }
  }
  return routerReplace.call(this, location, onComplete, onAbort)
}

Vue.use(Router)
export default new Router({
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    }
    return { x: 0, y: 0 }
  },
  mode: 'history',
  base: '/newb2c',
  routes: [
    {
      path: '/',
      redirect: '/home'
      // component: () => import('../douyin/Home/defaultHome.vue')
    },
    {
      path: '/home',
      name: i18n.t('router.home'),
      meta: {
        navShow: true,
        keepAlive: true
      },
      component: () => import('../douyin/Home/index.vue')
    },
    {
      path: '/activity',
      name: '列表',
      meta: {
        navShow: true,
        keepAlive: true
      },
      component: () => import('../douyin/Activity/index.vue')
    },
    {
      path: '/user',
      name: i18n.t('router.user'),
      meta: {
        fooShow: false,
        keepAlive: true,
        navShow: true
      },
      component: () => import('../douyin/User/index.vue')
    },
    { path: '*', redirect: '/home' } // 所有未匹配到的路由，都跳转首页
  ]
})
